package com.coffee.admin.service.supplier.impl;

import com.coffee.admin.api.supplier.query.SupplierRolePermissionQuery;
import com.coffee.admin.api.supplier.result.SupplierRolePermissionResult;
import com.coffee.admin.api.supplier.service.SupplierRolePermissionService;
import com.coffee.admin.service.supplier.bean.SupplierRolePermission;
import com.coffee.admin.service.supplier.dao.SupplierRolePermissionDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SupplierRolePermissionServiceImpl implements SupplierRolePermissionService {
	@Resource
	private SupplierRolePermissionDao	supplierRolePermissionDao;

	@Override
	public Page<SupplierRolePermissionResult> getPager(SupplierRolePermissionQuery param, PagerInfo pr) {
		Page<SupplierRolePermissionResult> pager=new Page<SupplierRolePermissionResult>(pr.getStart(), pr.getPageSize());
		Integer count=supplierRolePermissionDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<SupplierRolePermission> list=supplierRolePermissionDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, SupplierRolePermissionResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(SupplierRolePermissionQuery param){
		Integer count=supplierRolePermissionDao.count(param);
		return count;
	}
	
	@Override
	public SupplierRolePermissionResult getDetail(Integer id){
		SupplierRolePermission item=supplierRolePermissionDao.findById(id);
		if(item==null){
			return null;
		}
		SupplierRolePermissionResult result=new SupplierRolePermissionResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<SupplierRolePermissionResult> getList(SupplierRolePermissionQuery param, PagerInfo pr){
		List<SupplierRolePermission> list=supplierRolePermissionDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, SupplierRolePermissionResult.class);
	}
	
	@Override
	public List<SupplierRolePermissionResult> getList(SupplierRolePermissionQuery param){
		List<SupplierRolePermission> list=supplierRolePermissionDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SupplierRolePermissionResult.class);
	}
	
	@Override
	public List<SupplierRolePermissionResult> getList(){
		SupplierRolePermissionQuery param = new SupplierRolePermissionQuery();
		List<SupplierRolePermission> list=supplierRolePermissionDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SupplierRolePermissionResult.class);
	}


	@Override
	public void delete(Integer id) {
		supplierRolePermissionDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(SupplierRolePermissionResult result)throws BusinessException {
		SupplierRolePermission item=new SupplierRolePermission();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			supplierRolePermissionDao.insert(item);
		}else {
			SupplierRolePermission tmp=supplierRolePermissionDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			supplierRolePermissionDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,SupplierRolePermissionResult> getItemMap(Set<Integer> idSet){
		Map<Integer, SupplierRolePermissionResult> map=new HashMap<Integer, SupplierRolePermissionResult>();
		List<SupplierRolePermission> list=supplierRolePermissionDao.findByIds(StringUtil.setToList(idSet));
		List<SupplierRolePermissionResult> resultList=DTOUtils.convertList(list, SupplierRolePermissionResult.class);
		for(SupplierRolePermissionResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,SupplierRolePermissionResult> getItemMap(){
		Map<Integer, SupplierRolePermissionResult> map=new HashMap<Integer, SupplierRolePermissionResult>();
		SupplierRolePermissionQuery param=new SupplierRolePermissionQuery();
		List<SupplierRolePermission> list=supplierRolePermissionDao.page(param, 0, Integer.MAX_VALUE);
		List<SupplierRolePermissionResult> resultList=DTOUtils.convertList(list, SupplierRolePermissionResult.class);
		for(SupplierRolePermissionResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
